﻿/////////////////////////////////////////////////////////////////////////////////////////////
// <copyright file="OutlookLiveMailUser.cs" company="Microsoft Corporation">
//     Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
/////////////////////////////////////////////////////////////////////////////////////////////

namespace Microsoft.LiveAtEdu.Samples.Powershell.OutlookLive
{
    using System;
    using System.Collections.ObjectModel;
    using System.Management.Automation;

    /// <summary>
    /// Powershell commands for a creating mailuser.
    /// </summary>
    public class OutlookLiveMailUser
    {
        /// <summary>
        /// SessionRunspace instance.
        /// </summary>
        private readonly SessionRunspace sessionRunspace;

        /// <summary>
        /// Initializes a new instance of the OutlookLiveMailUser class.
        /// </summary>
        /// <param name="sessionRunspace">SessionRunspace instance.</param>
        public OutlookLiveMailUser(SessionRunspace sessionRunspace)
        {
            if (sessionRunspace == null)
            {
                throw new ArgumentOutOfRangeException("sessionRunspace");
            }

            this.sessionRunspace = sessionRunspace;
        }

        /// <summary>
        /// Create a new mail user
        /// </summary>
        /// <param name="name">Name of user.</param>
        /// <param name="firstName">First name</param>
        /// <param name="lastName">Last name.</param>
        /// <param name="displayName">Display name</param>
        /// <param name="externalEmail">external email id</param>
        /// <param name="password">User password</param>
        /// <param name="windowsLiveId">Windows live id.</param>
        /// <returns>Return true(pass) / false(fail)</returns>
        public bool CreateMailUser(string name, string firstName, string lastName, string displayName, string externalEmail, string password, string windowsLiveId)
        {
            string[] args = { name, firstName, lastName, displayName, externalEmail, password, windowsLiveId };

            var command = new PSCommand();
            command.AddScript(string.Format(MailUserScripts.CreateMailUserScript, args));

            var accounts = this.sessionRunspace.ExecuteCommand(command);

            if (accounts != null && accounts.Count == 1)
            {
                return true;
            }

            return false;
        }
    }
}
